package com.jiukuaidao.zhulin.module;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import java.util.List;

/**
 * Author : PaiTaSuo
 * Date : 2016/6/2
 * Description :
 */
public class CommPagerAdapter extends FragmentStatePagerAdapter {

    /**
     * 分类所属的fragment列表
     */
    private List<Fragment> mFragments = null;

    private List<String> mtitles = null;

    public CommPagerAdapter(List<Fragment> fragments, FragmentManager fm, List<String> titles) {
        super(fm);
        this.mtitles = titles;
        this.mFragments = fragments;
    }

    public void setFragments(List<Fragment> fragments) {
        this.mFragments = fragments;
    }

    public void setTitle(List<String> titles) {
        this.mtitles = titles;
    }

    @Override
    public Fragment getItem(int position) {
        if (mFragments.size() <= position) {
            return null;
        }

        return mFragments.get(position);

    }

    @Override
    public int getCount() {
        return null != mtitles ? mtitles.size() : 0;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        return null != mtitles ? mtitles.get(position) : null;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        /**
         * 这里千万不能加下面这句话，如果加了 界面来回切换时 会出现空白
         */
        // container.removeView(fragments.get(position).getView()); //
        // 移出viewpager两边之外的page布局
    }
}
